// NFC_06 [FrameWindow with OpenGL].nova // Using namespace declarations. using Library.NFC; using Library.OpenGL; // The application class. class FrameWindowWithOpenGLApp { // Application class's "main" function. public static void main( String[] args ) { // Create a new OpenGL window. MyGLWindow window = new MyGLWindow( "NFC_06 [FrameWindow with OpenGL]", 640, 480 ); // Show the window. window.show( true ); // Process the window's events. window.processEvents( ); } } // Custom OpenGL window class. class MyGLWindow : FrameWindow { // The window's OpenGL rendering context. private RenderingContext renderingContext; // Class constructor. public MyGLWindow( String name, int sizeX, int sizeY ) : FrameWindow( name, sizeX, sizeY ) { // Create an OpenGL rendering context. renderingContext = OpenGL.createContext( graphics ); // Make the new rendering context the current one. OpenGL.makeCurrent( graphics, renderingContext ); // Initialize OpenGL. OpenGL.glShadeModel( OpenGL.GL_SMOOTH ); OpenGL.glClearColor( 0.0f, 0.0f, 0.0f, 0.5f ); OpenGL.glClearDepth( 1.0 ); OpenGL.glEnable( OpenGL.GL_DEPTH_TEST ); } // On paint event handler. public virtual void onPaint( ) { // Clear the buffers. OpenGL.glClear( OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT ); // Reset the current matrix. OpenGL.glLoadIdentity( ); // Move into the scene. OpenGL.glTranslatef( 0.0f, 0.0f, -4.0f ); // Draw the scene. OpenGL.glBegin( OpenGL.GL_TRIANGLES ); OpenGL.glColor3f( 0.0f, 0.0f, 1.0f ); // Blue. OpenGL.glVertex3f( 0.0f, 1.0f, 0.0f ); // Top OpenGL.glColor3f( 1.0f, 0.0f, 0.0f ); // Red. OpenGL.glVertex3f( -1.0f, -1.0f, 0.0f ); // Bottom left. OpenGL.glColor3f( 0.0f, 1.0f, 0.0f ); // Green. OpenGL.glVertex3f( 1.0f, -1.0f, 0.0f ); // Bottom right. OpenGL.glEnd( ); // Swap the front and back buffers. OpenGL.swapBuffers( graphics ); } // On size event handler. public virtual void onSize( int sizeX, int sizeY ) { // Prevent a divide by zero error. if ( sizeY == 0 ) { // Set the height equal to one. sizeY = 1; } // Reset the current viewport. OpenGL.glViewport( 0, 0, sizeX, sizeY ); // Select the projection matrix. OpenGL.glMatrixMode( OpenGL.GL_PROJECTION ); // Reset the projection matrix. OpenGL.glLoadIdentity( ); // Calculate the aspect ratio of the window. OpenGL.gluPerspective( 45.0, (double)sizeX / (double)sizeY, 0.1, 100.0 ); // Select the modelview matrix. OpenGL.glMatrixMode( OpenGL.GL_MODELVIEW ); // Reset the modelview matrix. OpenGL.glLoadIdentity( ); } // On close event handler. public virtual void onClose( ) { // Set the current OpenGL context to null. OpenGL.makeCurrent( null, null ); // Delete the OpenGL context. OpenGL.deleteContext( renderingContext ); } }